using System.Collections.Generic;
using UnityEngine;

public class ThrowingObjects : MonoBehaviour
{
    [Header("Data to scriptableObject")] [SerializeField]
    private MyItem _currentItem;

    [SerializeField] private List<MyItem> _throwingObjectsList = new();


    [Header("Data")] [SerializeField] private Throw _currentThrow;

    [SerializeField] private List<Throw> _throwList = new();

    [Header("����״̬")] [SerializeField] private bool _isIKeyPressed;

    [SerializeField] private bool _isRotateKeyPress;

    [Header("Ͷ������")]
    //ʹ��velocity������
    [SerializeField]
    private float _throwSpeed = 8f;

    [SerializeField] private float _throwRotate; //Ͷ���Ƕ�
    [SerializeField] private Vector2 _throwRotateRangle;

    [Header("״̬")] [SerializeField] private bool _isThrow;

    public Rigidbody2D _rb { get; private set; }

    private void Awake()
    {
        _throwList.ForEach(m => { m.gameObject.SetActive(false); });
    }

    private void Start()
    {
        _throwRotate = 45f; //TODO:����Ϊʵ�ַ�Χ�ڵ�������
        _currentThrow.gameObject.SetActive(true);
        _rb = _currentThrow.GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.I)) _isIKeyPressed = true;
        if (_isIKeyPressed)
        {
            if (Input.GetKeyDown(KeyCode.W) && _throwRotate < 90f) _throwRotate += 1f;
            if (Input.GetKeyDown(KeyCode.S) && _throwRotate > 0f) _throwRotate -= 1f;
        }

        if (Input.GetKeyUp(KeyCode.I))
        {
            Throw();
            _isThrow = false;
        }
    }

    private void Throw()
    {
        PolarToRightangle();
        _rb.velocity = _throwRotateRangle;
    }

    private void PolarToRightangle()
    {
        _throwRotateRangle.x = Mathf.Cos(_throwRotate) * _throwSpeed;
        _throwRotateRangle.y = Mathf.Sin(_throwRotate) * _throwSpeed;
    }

    private void SwitchHoldThorwObject()
    {
        if (Input.GetKeyDown(KeyCode.Alpha7) || Input.GetKeyDown(KeyCode.Alpha8) || Input.GetKeyDown(KeyCode.Alpha9) ||
            Input.GetKeyDown(KeyCode.Alpha0))
        {
            _currentThrow.gameObject.SetActive(false);
            _currentThrow = _throwList[0];
            _currentThrow.gameObject.SetActive(true);
        }
    }
}